package com.jcooky.tutorfs.proxy.processor.scheduled;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.mina.core.session.IoSession;

import com.jcooky.tutorfs.proxy.processor.MessageProcessor;
import com.jcooky.tutorfs.proxy.processor.ProcessException;
import com.jcooky.tutorfs.proxy.protocol.AbstractMessage;

public class SimpleScheduledProcessor<T extends AbstractMessage> implements ScheduledProcessor<T> {
	private ExecutorService threadPool;
	
	private MessageProcessor<T> processor;
	
	public SimpleScheduledProcessor(MessageProcessor<T> processor) {
		this.processor = processor;
	}
	
	public void setThreadPool(ExecutorService threadPool) {
		this.threadPool = threadPool;
	}
	
	public void process(final IoSession session, final T requestMessage) throws ProcessException {
		threadPool.execute(new Runnable() {
			public void run() {
				try {
					processor.process(session, requestMessage);
				} catch (ProcessException e) {
					throw new RuntimeException(e);
				}
			}
		});
	}
}
